﻿Imports System.Management
Imports Microsoft.Win32
Imports System.Net.NetworkInformation
Imports System.Security
Imports System.IO

<Serializable()> _
Public Class IPConfiguration
#Region "Properties"
    Private mDisplayname As String
    Public Property DisplayName() As String
        Get
            Return mDisplayname
        End Get
        Set(ByVal value As String)
            mDisplayname = value
        End Set
    End Property

    Private mLanAdapter As String
    Public Property LanAdapter() As String
        Get
            Return mLanAdapter
        End Get
        Set(ByVal value As String)
            mLanAdapter = value
        End Set
    End Property


    Private mDHCP As Boolean
    Public Property DHCP() As Boolean
        Get
            Return mDHCP
        End Get
        Set(ByVal value As Boolean)
            mDHCP = value
        End Set
    End Property


    Private mIPAdresse As String
    Public Property IPAdresse() As String
        Get
            Return mIPAdresse
        End Get
        Set(ByVal value As String)
            mIPAdresse = value
        End Set
    End Property


    Private mSubnetzmaske As String
    Public Property Subnetzmaske() As String
        Get
            Return mSubnetzmaske
        End Get
        Set(ByVal value As String)
            mSubnetzmaske = value
        End Set
    End Property


    Private mGateway As String
    Public Property Gateway() As String
        Get
            Return mGateway
        End Get
        Set(ByVal value As String)
            mGateway = value
        End Set
    End Property


    Private mDNS1 As String
    Public Property DNS1() As String
        Get
            Return mDNS1
        End Get
        Set(ByVal value As String)
            mDNS1 = value
        End Set
    End Property


    Private mDNS2 As String
    Public Property DNS2() As String
        Get
            Return mDNS2
        End Get
        Set(ByVal value As String)
            mDNS2 = value
        End Set
    End Property


    Private mProxy As Boolean
    Public Property Proxy() As Boolean
        Get
            Return mProxy
        End Get
        Set(ByVal value As Boolean)
            mProxy = value
        End Set
    End Property


    Private mProxyIP As String
    Public Property ProxyIP() As String
        Get
            Return mProxyIP
        End Get
        Set(ByVal value As String)
            mProxyIP = value
        End Set
    End Property


    Private mProxyPort As String
    Public Property ProxyPort() As String
        Get
            Return mProxyPort
        End Get
        Set(ByVal value As String)
            mProxyPort = value
        End Set
    End Property


    Private mProxyBypassLocal As Boolean
    Public Property ProxyBypassLocal() As Boolean
        Get
            If mProxyBypassLocal = Nothing Then
                Return False
            End If
            Return mProxyBypassLocal
        End Get
        Set(ByVal value As Boolean)
            mProxyBypassLocal = value
        End Set
    End Property


    Private mProxyExceptions As String
    Public Property ProxyExceptions() As String
        Get
            Return mProxyExceptions
        End Get
        Set(ByVal value As String)
            mProxyExceptions = value
        End Set
    End Property


    Private mProxyFFPrefsPath As String
    Public Property ProxyFFPrefsPath() As String
        Get
            Return mProxyFFPrefsPath
        End Get
        Set(ByVal value As String)
            mProxyFFPrefsPath = value
        End Set
    End Property


    Private mScript As String
    Public Property Script() As String
        Get
            Return mScript
        End Get
        Set(ByVal value As String)
            mScript = value
        End Set
    End Property

#End Region
#Region "Constructors"
    Public Sub New()
        Me.New("<Profile>")
        Me.DHCP = True
        FindFirefoxProfile()
    End Sub
    Public Sub New(ByVal pTitle As String)
        DisplayName = pTitle
        DHCP = True
        FindFirefoxProfile()
    End Sub

#End Region
#Region "Methods"

    Private Sub FindFirefoxProfile()
        Dim tPath As New DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\Mozilla\Firefox\Profiles\")
        Try
            If tPath.GetDirectories("*.default")(0).Exists Then
                ProxyFFPrefsPath = tPath.GetDirectories("*.default")(0).FullName
            End If
        Catch ex As Exception

        End Try

    End Sub

    Public Overrides Function ToString() As String
        Return DisplayName
    End Function

    Public Sub ApplyConfiguration()
        setNetworkConfiguration()
        setProxyFF()
        setProxyIE()
        ExecuteScript()
    End Sub

    Private Sub setNetworkConfiguration()
        If DHCP Then
            SetDHCP()
        Else
            SetIP()
        End If
    End Sub

    Private Sub setProxyIE()
        Dim _RegistryKey As RegistryKey
        _RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", True)
        If Proxy Then
            _RegistryKey.SetValue("ProxyEnable", "1", RegistryValueKind.DWord)
            If ProxyBypassLocal Then
                _RegistryKey.SetValue("ProxyOverride", ProxyExceptions + ";<local>", RegistryValueKind.String)
            Else
                _RegistryKey.SetValue("ProxyOverride", ProxyExceptions, RegistryValueKind.String)
            End If
            If ProxyIP IsNot Nothing AndAlso ProxyPort IsNot Nothing Then
                _RegistryKey.SetValue("ProxyServer", ProxyIP + ":" + ProxyPort, RegistryValueKind.String)
            End If

        Else
            _RegistryKey.SetValue("ProxyEnable", "0", RegistryValueKind.DWord)
        End If
       
    End Sub

    Private Sub setProxyFF()
        If ProxyFFPrefsPath IsNot Nothing Then
            Dim tPrefsFileTMP As String = ""
            Dim tPrefsFileNew As String = ""
            Dim lProxyFFUserPath As String = ProxyFFPrefsPath + "\user.js"

            If Proxy Then
                Dim tSettings As String = "network.proxy.ftp;network.proxy.ftp_port;network.proxy.gopher;network.proxy.gopher_port;network.proxy.http;network.proxy.http_port;network.proxy.ssl;network.proxy.ssl_port"
                For Each setting As String In tSettings.Split(";"c)
                    If setting.IndexOf("port") >= 0 Then
                        tPrefsFileNew += "user_pref(""" + setting + """, """ + ProxyPort + """);" + Environment.NewLine
                    Else
                        tPrefsFileNew += "user_pref(""" + setting + """, """ + ProxyIP + """);" + Environment.NewLine
                    End If
                Next
                tPrefsFileNew += "user_pref(""network.proxy.type"", 1);"
            Else
                tPrefsFileNew += "user_pref(""network.proxy.type"", 4);"
            End If
            IO.File.Delete(lProxyFFUserPath)
            IO.File.WriteAllText(lProxyFFUserPath, tPrefsFileNew, New System.Text.UTF8Encoding)
        End If
    End Sub
    Private Sub ExecuteScript()
        If Script IsNot Nothing Then
            Dim lTMPBatFile As String = "c:\temp.bat"
            Try
                IO.File.Delete(lTMPBatFile)
            Catch ex As Exception

            End Try
            IO.File.WriteAllText("C:\temp.bat", Script)
            System.Diagnostics.Process.Start(lTMPBatFile)

        End If
    End Sub
    Private Sub SetDHCP()
        Dim mc As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
        Dim moc As ManagementObjectCollection = mc.GetInstances()
        For Each mo As ManagementObject In moc
            If mo.Item("IPEnabled") = True And mo.Item("Servicename") <> "NdisIP" And mo.Item("Description") = LanAdapter Then
                Dim enableDHCP As ManagementBaseObject = mo.InvokeMethod("EnableDHCP", Nothing, Nothing)
            End If
        Next
    End Sub

    Private Sub SetIP()
        Dim mc As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
        Dim moc As ManagementObjectCollection = mc.GetInstances()
        For Each mo As ManagementObject In moc
            If mo.Item("IPEnabled") = True And mo.Item("Servicename") <> "NdisIP" And mo.Item("Description") = LanAdapter Then
                Dim newIP As ManagementBaseObject = mo.GetMethodParameters("EnableStatic")
                Dim newGate As ManagementBaseObject = mo.GetMethodParameters("SetGateways")
                Dim newDNS As ManagementBaseObject = mo.GetMethodParameters("SetDNSServerSearchOrder")
                newGate.Item("DefaultIPGateway") = Gateway.Split(","c)
                newGate.Item("GatewayCostMetric") = "1".ToCharArray
                newIP.Item("IPAddress") = IPAdresse.Split(","c)
                newIP.Item("SubnetMask") = Subnetzmaske.Split(","c)
                mo.InvokeMethod("EnableStatic", newIP, Nothing)
                mo.InvokeMethod("SetGateways", newGate, Nothing)
                If DNS1 IsNot Nothing AndAlso DNS1 <> "" Then
                    If DNS2 IsNot Nothing AndAlso DNS2 <> "" Then
                        newDNS.Item("DNSServerSearchOrder") = (DNS1 + "," + DNS2).Split(","c)
                    Else
                        newDNS.Item("DNSServerSearchOrder") = DNS1.Split(","c)
                    End If
                    mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, Nothing)
                End If
            End If
        Next mo
    End Sub

#End Region
End Class
